<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Personal extends CI_Controller
{
	private $user_id;
	function __construct()
	{
		parent::__construct();
		
		$this->load->config('layout', TRUE);
		$this->load->config('coplay', TRUE);
		$this->load->helper('url');
		$this->load->library('security');
		$this->load->library('session');
		$this->load->library('form_validation');
		$this->load->library('tank_auth');
		$this->load->library('mahana_messaging');
		$this->lang->load('tank_auth');
		$this->lang->load('personal');
		$this->load->model('activity_model');
		$this->load->model('personal_info');
		
		$this->user_id = $this->session->userdata('user_id');
		
		
	}

	function index()
	{
		if (!$this->tank_auth->is_logged_in()) {//not logged in
			redirect();
		}
		else{
			$data = $this->session->all_userdata();
			$data['user'] = $this->users->get_user_by_id($this->user_id,1);
		}
		
		$this->load->view('coplay/header', $data);
		$data['action_msg'] = $this->session->flashdata('action_msg');
		$data['activities'] = $this->activity_model->get($this->user_id);
		//echo "<pre>";print_r($data['activities']);echo "</pre>";
		$data['activity_organize'] = $this->load->view('coplay/personal/activity_organize', $data ,true);
		$data['activity_favorite'] = $this->load->view('coplay/personal/activity_favorite', $data ,true);
		$data['activity_join'] = $this->load->view('coplay/personal/activity_join', $data ,true);
		$data['user_profile'] = $this->load->view('coplay/personal/user_profile', $data ,true);
		$data['message_box'] = $this->load->view('coplay/personal/message_box', $data ,true);
		$data['change_password_form'] = $this->load->view('coplay/personal/change_password_form', '' ,true);
		$this->load->view('coplay/personal/index', $data);
		
	}
	
	function update_profile(){
		//echo "<pre>";print_r($this->input->post());echo "</pre>";exit;
		if (!$this->tank_auth->is_logged_in()) {//not logged in
			redirect();
		}
		else{
			$data = array();
			$this->form_validation->set_rules('username','匿稱','trim|required|xss_clean');
			$this->form_validation->set_rules('email','Email','trim|required|xss_clean|valid_email');
			$this->form_validation->set_rules('user_birthdate','生日', 'trim|xss_clean|valid_date');
			$this->form_validation->set_rules('user_sex','性別', 'trim|xss_clean|required');
			$this->form_validation->set_rules('user_phone','電話', 'trim|xss_clean|is_natural');
			$this->form_validation->set_rules('user_address','住址', 'trim|xss_clean|');
			$this->form_validation->set_rules('country','國家', 'trim|xss_clean|');
			$this->form_validation->set_rules('city','城市', 'trim|xss_clean|');
			if ($this->form_validation->run()) {
				if($this->tank_auth->is_update_email_available($this->input->post('email'),3)){
					if($this->personal_info->update_profile($this->input->post(),$this->user_id)){
						$update_response = array('update_status'=>'1','view'=>$this->load->view('coplay/personal/user_profile', '', true));
						echo json_encode($update_response);	
					}
					else {
						$update_response = array('update_status'=>'0','view'=>$this->lang->line('auth_email_in_use'));
						echo json_encode($update_response);	
					}	
				}
				else{
					$data['errors'] = array();
					$data['errors']['email'] = $this->lang->line('auth_email_in_use');
					$update_response = array('update_status'=>'0','view'=>$this->load->view('coplay/personal/user_profile', $data, true));
					echo json_encode($update_response);
				}
			}
			else{
				
				$update_response = array('update_status'=>'0','view'=>$this->load->view('coplay/personal/user_profile', '', true));
				echo json_encode($update_response);
			}	
		}
	}
	
	function change_password()
	{
		sleep(1);
		if (!$this->tank_auth->is_logged_in()) {								// not logged in or not activated
			redirect('');

		} else {
			$this->form_validation->set_rules('old_password', '舊密碼', 'trim|required|xss_clean');
			$this->form_validation->set_rules('new_password', '新密碼', 'trim|required|xss_clean|min_length['.$this->config->item('password_min_length','tank_auth').']|max_length['.$this->config->item('password_max_length', 'tank_auth').']|alpha_dash');
			$this->form_validation->set_rules('confirm_new_password', '確認新密碼', 'trim|required|xss_clean|matches[new_password]');

			$data['errors'] = array();

			if ($this->form_validation->run()) {								// validation ok
				if ($this->tank_auth->change_password(
						$this->form_validation->set_value('old_password'),
						$this->form_validation->set_value('new_password'))) {	// success
					
					$update_response = array('update_status'=>'1','view'=>$this->load->view('coplay/personal/change_password_form', '', true));
					echo json_encode($update_response);	exit;

				} else {														// fail
					$errors = $this->tank_auth->get_error_message();
					foreach ($errors as $k => $v)	$data['errors'][$k] = $this->lang->line($v);
				}
			}

			$update_response = array('update_status'=>'0','view'=>$this->load->view('coplay/personal/change_password_form', $data, true));
			echo json_encode($update_response);	
		}
	}
	
	function send_message(){
		sleep(1);
		if (!$this->tank_auth->is_logged_in()) {									// logged in
			$response = array('send_status'=>'0','reload_page'=>'1','view'=>$this->lang->line('please_login'));
			echo json_encode($response);
		}
		else{
			$message = $this->input->post(NULL,true);
			if(!isset($message['sendto'])){
				$this->form_validation->set_rules('receivers','收件者','trim|required|xss_clean');
				//$this->form_validation->set_rules('subject','標題','trim|required|xss_clean');
				$this->form_validation->set_rules('body','信件內容', 'trim|required|xss_clean');
				if ($this->form_validation->run()) {
					
					$recipient_ids = $this->mahana_messaging->mapping_recipient_id($message['receivers'],$this->user_id);
					
					$status = $this->mahana_messaging->send_new_message($this->user_id,$recipient_ids,$message['body']);
					$status['view'] = $status['msg'];
					echo json_encode($status);exit;
				}
				$response = array('send_status'=>'0','view'=>$this->load->view('coplay/personal/send_message_form', $message, true));
				echo json_encode($response);
			}
			else {
				$response = array('send_status'=>'0','view'=>$this->load->view('coplay/personal/send_message_form', $message, true));
				echo json_encode($response);			
			}
			
		}
	}
	
	function reply_message(){
		if (!$this->tank_auth->is_logged_in()) {									// logged in
			$response = array('send_status'=>'0','reload_page'=>'1','view'=>'');
			echo json_encode($response);
		}
		else{
			$message = $this->input->post(NULL,true);
			$status = $this->mahana_messaging->send_new_message($this->user_id,$message['recipient_id'],$message['body']);
			if($status['err']=='0'){//message sent
				$response = array('send_status'=>'1','reload_page'=>'0','msg'=>$status['msg']);	
			}
			
			
			echo json_encode($status);exit;
		}
	}

	function get_messages_by_sender(){
		//echo "<pre>";print_r($this->input->post(NULL,true));echo "</pre>";
		$data = $this->input->post(NULL,true);
		if (!$this->tank_auth->is_logged_in()) {									// logged in
			$response = array('status'=>'0','reload_page'=>'1','view'=>'');
			echo json_encode($response);
		}
		else{
			$all_threads_grouped_by_user  = $this->mahana_messaging->get_all_msgs_grouped_by_sender($this->user_id, $data['sender_id']);
			//echo "<pre>";print_r($all_threads_grouped_by_user);echo "</pre>";
			$all_threads_grouped_by_user['recipient_id'] = $data['sender_id'];
			$response = array('status'=>'1','reload_page'=>'1','view'=> $this->load->view('coplay/personal/messages', $all_threads_grouped_by_user, true));
			echo json_encode($response);
		}
	}
}